#include <arch/types.h>
#include <arch/CPU.h>
#include <arch/Atomic.h>
#include <ipc/Spinlock.h>

Spinlock::Spinlock() {
	Init();
}

void Spinlock::Init() {
	lock = 0;
	intState = false;
}

void Spinlock::Lock() {
	intState = CPU::DisableInterrupts();
	while (Atomic::TestAndSet(&lock)) {
		while (lock);
	}
}

void Spinlock::Unlock() {
	lock = 0;
	CPU::RestoreInterrupts(intState);
}
